package com.fyzk.activity.forum;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.tyjc.lottery.R;
import com.fyzk.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanxs11 on 17/5/5.
 */

public class FocusView extends LinearLayout {

    private Context context;

    private View view;

    private TextView focusCount_tv;
    private Button addFocus_btn;
    private HorizontalListView gridView;
    private FocusGvAdapter adapter;

    private List<ForumFocusBean> infos = new ArrayList<>();

    public FocusView(Context context) {
        super(context);
        this.context = context;

        initView();
    }

    public void initView() {
        view = LayoutInflater.from(context).inflate(R.layout.view_forum_focus, null);
        focusCount_tv = (TextView) view.findViewById(R.id.forum_focus_count);
        addFocus_btn = (Button) view.findViewById(R.id.forum_focus_add);
        gridView = (HorizontalListView) view.findViewById(R.id.forum_focus_gv);
        adapter = new FocusGvAdapter();
        gridView.setAdapter(adapter);

        addFocus_btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ForumListActivity.class);
                context.startActivity(intent);
            }
        });

        this.addView(view);
    }

    public void setInfo(List<ForumFocusBean> infos) {
        this.infos = infos;
        LogUtil.d("size:"+infos.size());
        if (infos != null) {
            focusCount_tv.setText(infos.size()+"");
            if (infos.size() > 0) {
                gridView.setVisibility(View.VISIBLE);
                gridView.notifyDataSetChanged(adapter);
            } else {
                gridView.setVisibility(View.GONE);
            }
        }
    }

    class FocusGvAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return infos.size();
        }

        @Override
        public Object getItem(int position) {
            return infos.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            ForumFocusBean info = infos.get(position);
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.view_forum_focus_item, null);
                viewHolder.title = (TextView) convertView.findViewById(R.id.forum_focus_item_title);
                viewHolder.icon = (ImageView) convertView.findViewById(R.id.forum_focus_item_icon);

                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.title.setText(info.getTopic_title());
//            viewHolder.icon.setImageUrl(info.getTopic_pic(), App.getInstance().getImageLoader());
            Glide.with(context).load(info.getTopic_pic())
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .into(viewHolder.icon);

            convertView.setClickable(true);
            convertView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, TopicListActivity.class);
                    intent.putExtra("topic_id", infos.get(position).getTopic_id());
                    intent.putExtra("title", infos.get(position).getTopic_title());
                    context.startActivity(intent);
                }
            });
            return convertView;
        }
    }

    class ViewHolder {
        TextView title;
        ImageView icon;
    }
}
